package com.sunny.l23;

import java.util.Iterator;
import java.util.TreeSet;

public class Solution {
    private TreeSet<Integer> set = new TreeSet<>();

    public static void main(String[] args) {

        Solution solution = new Solution();
        int[] num = {9,1,-3,2,4,8,3,-1,6,-2,-4,7};
        System.out.println(solution.longestConsecutive(num));

    }

    public int longestConsecutive(int[] num) {
        for(int tmp: num){
            set.add(tmp);
        }
        Iterator<Integer> iter = set.iterator();
        int res = 1;
        int tmpRes = 1;
        int prev = iter.next();
//        System.out.println(prev);
        while (iter.hasNext()){
            int n = iter.next();
//            System.out.println(n);
            if(prev + 1 == n){
                tmpRes++;
            }else if(1 != tmpRes){
                if(res < tmpRes){
                    res = tmpRes;
                }
                tmpRes = 1;
            }
            prev = n;
        }
        if(tmpRes != 1){
            if(res < tmpRes)
                res = tmpRes;
        }
        return res;
    }
}